﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class NetWorkControl : NetworkManager
{
    #region ClientHandler
    IClientHandler loginRequestHandler;
    IClientHandler registerRequestHandler;
    #endregion

    #region ServerHandler
    IServerHandler loginResponse;
    IServerHandler registerResponse;
    #endregion
    public override void Start()
    {
        registerRequestHandler = new LoginRequestHandler();
        loginRequestHandler = new RegisterRequestHandler();
        loginResponse = new LoginResponseHanlder();
        registerResponse = new RegisterResponseHandler();
        StartServer();
        // yield return new WaitForSeconds(1.0f);
        StartClient();
    }

    public override void OnStartServer()
    {
        base.OnStartServer();
        NetworkServer.RegisterHandler<NetMessage>(OnServerRecieve, true);

    }
    #region 服务端接收到消息
    private void OnServerRecieve(NetworkConnection conn, NetMessage message)
    {

        int areaCode = message.AreaCode;
        Debug.Log("接收到客户端消息 message.ReturnCode" + message.Value);

        switch (areaCode)
        {
            case AreaCode.Account:
                ProcessAccount(conn, message);
                break;

            default:
                break;
        }

    }

    /// <summary>
    /// 处理账号相关操作
    /// </summary>
    /// <param name="conn"></param>
    /// <param name="message"></param>
    void ProcessAccount(NetworkConnection conn, NetMessage message)
    {
        byte opCode = message.AreaCode;
        // NetMsg netMsg = ChangeMessage(message);
        switch (opCode)
        {
            case OperationCode.Login:
                //NetMsg msg = ChangeMessage(message);
                //Debug.Log("在接口方法前打印msg.Value" +  msg.Value);
                loginResponse.OnRecieveMsg(conn, message);
                break;

            case OperationCode.Register:
                registerResponse.OnRecieveMsg(conn, message);
                break;
            default:
                break;
        }
    }

    #endregion


    #region 客户端收到消息
    /// <summary>
    /// 注册事件的入口函数
    /// </summary>
    public override void OnStartClient()
    {
        base.OnStartClient();
        NetworkClient.RegisterHandler<NetMessage>(OnClientRecieved, true);
    }

    /// <summary>
    /// 客户端接收到消息的分发器
    /// </summary>
    /// <param name="msg"></param>
    private void OnClientRecieved(NetMessage msg)
    {
        byte areaCode = msg.AreaCode;
        Debug.Log("接收到服务器消息:" + areaCode);
        switch (areaCode)
        {
            case AreaCode.Account:

                break;

            default:
                break;
        }
    }
    #endregion

    public override void OnServerConnect(NetworkConnection conn)
    {
        base.OnServerConnect(conn);
    }
    public override void OnClientConnect(NetworkConnection conn)
    {
        base.OnClientConnect(conn);

        Debug.Log("打印客户端已连接" + conn.address);
    }

    public override void OnClientDisconnect(NetworkConnection conn)
    {
        base.OnClientDisconnect(conn);
        Debug.Log("打印客户端已断开" + conn.address);

    }



    NetMsg ChangeMessage(NetMessage net)
    {
        NetMsg netMsg = new NetMsg()
        {
            AreaCode = net.AreaCode,
            //SubCode = net.SubCode,
            //OperationCode = net.SubCode,
            //Value=net.Value,
            //ReturnCode = net.ReturnCode,


        };
        return netMsg;
    }

}


public static class NetMessageWriter
{
    public static void WriteNetMessage(this NetworkWriter writer, NetMessage msg)
    {
        writer.WriteByte(msg.AreaCode);
        writer.WriteString(msg.Value);
    }

    public static NetMessage ReadNetMessage(this NetworkReader reader)
    {
        return new NetMessage
        {
            AreaCode = reader.ReadByte(),
            Value = reader.ReadString(),
        };
    }
}
#region MyRegion
public struct NetMessage : NetworkMessage
{


    /// <summary>
    /// 区域码
    /// </summary>
    public byte AreaCode { get; set; }

    ///// <summary>
    ///// 操作码
    ///// </summary>
    //public byte OperationCode { get; set; }

    ///// <summary>
    ///// 子操作码
    ///// </summary>
    //public byte SubCode { get; set; }

    ///// <summary>
    ///// 返回码
    ///// </summary>
    //public byte ReturnCode { get; set; }

    public string Value { get; set;}

}
#endregion




